﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HMI.Header
{
    /// <summary>
    /// SwitchBtn.xaml 的交互逻辑
    /// </summary>
    public partial class SwitchBtn : UserControl
    {
        public SwitchBtn()
        {
            InitializeComponent();
        }
        public Action<int> SwitchBtnEvent;
        public int switchvalue = 0;
        public bool Checked
        {
            get
            {
                if (switchvalue == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            set
            {
                SolidColorBrush brushbkc = FindResource("BorderColor1") as SolidColorBrush;
                SolidColorBrush brushbkcx = FindResource("BackxColor") as SolidColorBrush;
                SolidColorBrush brushbfr = FindResource("Lightx") as SolidColorBrush;
                SolidColorBrush brushbfrx = FindResource("ForeColor") as SolidColorBrush;
                if (value)
                {
                    switchvalue = 1;
                    fbtn.Background = brushbkc;
                    rbtn.Background = brushbfr;
                    flab.Visibility = Visibility.Hidden;
                    rlab.Visibility = Visibility.Visible;
                    rlab.Foreground = brushbkc;
                }
                else
                {
                    switchvalue = 0;
                    fbtn.Background = brushbkc;
                    rbtn.Background = brushbkcx;
                    flab.Visibility = Visibility.Visible;
                    rlab.Visibility = Visibility.Hidden;
                    flab.Foreground = brushbfrx;
                }
            }
        }
        public string TrueLabel
        {
            get { return Convert.ToString(rlab.Content); }
            set { rlab.Content = value; }
        }
        public string FalseLabel
        {
            get { return Convert.ToString(flab.Content); }
            set { flab.Content = value; }
        }
        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (switchvalue == 0)
            {
                Checked = true;
            }
            else
            {
                Checked = false;
            }
            if (SwitchBtnEvent != null)
                SwitchBtnEvent(switchvalue);
            RoutedEventArgs args = new RoutedEventArgs(Switch);
            this.OnSwitch(args);

        }
        public static readonly RoutedEvent Switch = EventManager.RegisterRoutedEvent("SwitchEvent", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SwitchBtn));
        public event RoutedEventHandler SwitchEvent
        {
            add { this.AddHandler(Switch, value); }
            remove { this.RemoveHandler(Switch, value); }
        }
        public void OnSwitch(RoutedEventArgs e)
        {
            this.RaiseEvent(e);
        }
    }
}
